<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="24">
          <el-form-item :label="$t('图片类型')" prop="imgType">
            <dict-select
              v-model="form.imgType"
              dictionary="real_estate_picture_type"
              :placeholder="$t('图片类型')"
            />
          </el-form-item>
          <el-col :span="24">
            <el-form-item :label="$t('备注')" prop="remark">
              <el-input v-model="form.remark" type="textarea" :maxlength="2000" show-word-limit :placeholder="$t('备注')" />
            </el-form-item>
          </el-col>
        </el-col>
      </el-row>
      <FileTable :list.sync="form.fileList" data-type="deal_picture" :business-id="autoBusinessId($route.query.id, form)" :specify-file-suffix="['jpg', 'png', 'jpeg']" :specify-file-size="10" @deleteSingleFile="deleteSingleFile" />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存取消按钮-->
      <save-btn has-permi="deal:picture" />
    </div>
  </div>
</template>

<script>
import { saveDealPicture, getDealPicture, deleteByFileId } from '@/api/deal/estate/picture/dealPicture'

export default {
  comments: {
    fileTable: () => import('@/components/File/FileTable/index.vue')
  },
  data() {
    return {
      // 表单参数
      form: {},
      // 遮罩层
      loading: false,
      // 表单校验
      rules: {
        dealId: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        imgType: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        fileId: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        thumbnailId: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getDicts('real_estate_picture_type').then(response => {
      this.imgTypeOptions = response.data
    })
  },
  methods: {
    // 初始化数据
    init(data) {
      this.loading = true
      if (data != null && data.id !== undefined) {
        getDealPicture(data.id).then(response => {
          this.form = response.data
          this.loading = false
        })
      } else {
        this.reset()
        this.loading = false
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        dealId: this.$route.query.id,
        imgType: undefined,
        fileId: undefined,
        thumbnailId: undefined,
        other: undefined,
        remark: undefined,
        createTime: undefined,
        updateTime: undefined,
        createBy: undefined,
        updateBy: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        var flag = true
        if (this.form.fileList === undefined || this.form.fileList === null || this.form.fileList === '') {
          this.msgError(this.$t('请先上传附件'))
          flag = false
        }
        if (valid && flag) {
          this.loading = true
          saveDealPicture(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.closeForm()
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      })
    },
    deleteSingleFile(fileId) {
      var that = this
      deleteByFileId(fileId).then(() => {
        if (that.$parent.$parent.getList !== undefined) {
          that.$parent.$parent.getList()
        }
      })
    }
  }

}
</script>
